package com.example.config;

import com.example.untils.AESUtil;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/files/*")
public class FileServlet extends HttpServlet {

    private String uploadDir;

    public FileServlet(String uploadDir) {
        this.uploadDir = uploadDir;
    }

    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException{
//        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-:: " + uploadDir);
        // 获取请求的文件路径
        String filePath = request.getPathInfo();
        Integer size = getSize(uploadDir + filePath);
        // 解密文件内容
        byte[] decryptedContent = new byte[size];
        try {
            decryptedContent = AESUtil.decrypt(uploadDir + filePath);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
//        System.out.println("111111111222222222222222233333333333333333");
        // 找到最后一个#的位置
        int lastIndex = filePath.lastIndexOf(".");
        String type = "";
        if (lastIndex != -1) {
            // 获取最后一个#后面的文字
            type = filePath.substring(lastIndex + 1);
//            System.out.println("最后一个#后面的文字是：" + type);
        }

        String contentType = "";
        switch (type.toLowerCase()) {
            case "ppt":
            case "pptx":
                contentType = "application/vnd.ms-powerpoint";
                break;
            case "xls":
            case "xlsx":
                contentType = "application/vnd.ms-excel";
                break;
            case "pdf":
                contentType = "application/pdf";
                break;
            case "jpg":
            case "jpeg":
                contentType = "image/jpeg";
                break;
            case "png":
                contentType = "image/png";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "bmp":
                contentType = "image/bmp";
                break;
            default:
                contentType = "application/octet-stream"; // 默认二进制流类型
        }
        // 设置响应内容类型
        response.setContentType(contentType);
//        response.setContentType("image/jpeg");
//        System.out.println("fdsagasdgsadgsdagdsag\nfdasfdsfsadfsadgdsgdfshd\ngfdgrhdrhershesrhershesrh\n");

        // 将解密后的内容写入响应输出流
        response.getOutputStream().write(decryptedContent);
    }

    private Integer getSize(String filePath) throws IOException {
        byte[] encryptedBytes = Files.readAllBytes(Paths.get(filePath));
        return encryptedBytes.length;
    }
}
